package xcmg.device.service.secondAgent;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.secondAgent.OemInfoDO;
import xcmg.device.dao.entity.secondAgent.SecondAgentDO;
import xcmg.device.dao.entity.secondAgent.SecondAgentInfoDO;
import xcmg.device.dao.entity.secondAgent.SecondAgentSelectedDO;
import xcmg.device.dao.mapper.secondAgent.SecondAgentMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.AuthConditionVO;
import xcmg.device.service.vo.company.CompanyBaseInfo;
import xcmg.device.service.vo.secondAgent.OemInfoVO;
import xcmg.device.service.vo.secondAgent.SecondAgentConditionVO;
import xcmg.device.service.vo.secondAgent.SecondAgentInfoVO;
import xcmg.device.service.vo.secondAgent.SecondAgentVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SecondAgentService {

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private SecondAgentMapper secondAgentMapper;

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(List<SecondAgentVO> secondAgentVOs, String userId, String orgId) throws Exception {
        for (SecondAgentVO secondAgentVO : secondAgentVOs) {
            ErrorCode errorCode = addSingle(secondAgentVO, userId, orgId);
            if (ErrorCode.Success.getCode() != errorCode.getCode()) {
                throw new Exception(secondAgentVO.getOrgId() + ":添加失败");
            }
        }
        return ErrorCode.Success;
    }

    private ErrorCode addSingle(SecondAgentVO secondAgentVO, String userId, String orgId) throws Exception {
        if (StringUtils.isEmpty(secondAgentVO.getOrgId())) {
            throw new Exception("企业ID不能为空");
        }
        SecondAgentDO secondAgentDO = new SecondAgentDO();
        BeanUtils.copyProperties(secondAgentVO, secondAgentDO);
        secondAgentDO.setOemOrgId(orgId);
        int count = secondAgentMapper.getCount(secondAgentDO);
        if (count > 0) {
            throw new Exception("代理商已存在");
        }
        Date currentTime = new Date();
        secondAgentDO.setId(getId());
        secondAgentDO.setCreateBy(userId);
        secondAgentDO.setCreateTime(currentTime);
        secondAgentDO.setUpdateBy(userId);
        secondAgentDO.setUpdateTime(currentTime);
        int resultCount = secondAgentMapper.insert(secondAgentDO);
        if (resultCount < 1) {
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    public ErrorCode delete(String id, String userId) {
        SecondAgentDO secondAgentDO = new SecondAgentDO();
        secondAgentDO.setId(id);
        secondAgentDO.setUpdateBy(userId);
        secondAgentDO.setUpdateTime(new Date());
        secondAgentMapper.delete(secondAgentDO);
        return ErrorCode.Success;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode delBatch(List<String> ids, String userId) {
        for (String id : ids) {
            delete(id, userId);
        }
        return ErrorCode.Success;
    }

    public PageCommonVO list(SearchCommonVO<SecondAgentConditionVO> condition, String orgId) {
        condition.getFilters().setOemOrgId(orgId);
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageHelperPlus.orderBy("s.create_time desc");
        List<SecondAgentInfoDO> secondAgentInfoDOs = secondAgentMapper.list(condition.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(secondAgentInfoDOs));
        List<SecondAgentInfoVO> secondAgentInfoVOs = new ArrayList<SecondAgentInfoVO>();
        if (!CollectionUtils.isEmpty(secondAgentInfoDOs)) {
            for (SecondAgentInfoDO secondAgentInfoDO : secondAgentInfoDOs) {
                SecondAgentInfoVO secondAgentInfoVO = new SecondAgentInfoVO();
                BeanUtils.copyProperties(secondAgentInfoDO, secondAgentInfoVO);
                secondAgentInfoVOs.add(secondAgentInfoVO);
            }
        }
        pageCommonVO.setPageInfoList(secondAgentInfoVOs);
        return pageCommonVO;
    }

    public List<OemInfoVO> listOemInfos(String orgId) {
        List<OemInfoDO> oemInfoDOs = secondAgentMapper.listOemInfos(orgId);
        List<OemInfoVO> oemInfoVOs = new ArrayList<OemInfoVO>();
        if (!CollectionUtils.isEmpty(oemInfoDOs)) {
            for (OemInfoDO oemInfoDO : oemInfoDOs) {
                OemInfoVO oemInfoVO = new OemInfoVO();
                BeanUtils.copyProperties(oemInfoDO, oemInfoVO);
                oemInfoVOs.add(oemInfoVO);
            }
        }
        return oemInfoVOs;
    }

    public SecondAgentVO getAgentInfo(String oemOrgId, String orgId) {
        SecondAgentDO secondAgentDO = secondAgentMapper.getAgentInfo(oemOrgId, orgId);
        if (secondAgentDO == null) {
            return null;
        }
        SecondAgentVO secondAgentVO = new SecondAgentVO();
        BeanUtils.copyProperties(secondAgentDO, secondAgentVO);
        return secondAgentVO;
    }

    public PageCommonVO getSecondProxyList(SearchCommonVO<AuthConditionVO> condition) {
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        PageHelperPlus.orderBy("s.create_time desc");
        List<SecondAgentSelectedDO> secondAgentSelectedDOs = secondAgentMapper.getSecondProxyList(condition.getFilters());
        PageCommonVO pageCommonVO = new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(secondAgentSelectedDOs));
        List<CompanyBaseInfo> companyBaseInfos = new ArrayList<CompanyBaseInfo>();
        if (!CollectionUtils.isEmpty(secondAgentSelectedDOs)) {
            for (SecondAgentSelectedDO secondAgentSelectedDO : secondAgentSelectedDOs) {
                CompanyBaseInfo companyBaseInfo = new CompanyBaseInfo();
                companyBaseInfo.setCompanyName(secondAgentSelectedDO.getComapnyName());
                companyBaseInfo.setOrgId(secondAgentSelectedDO.getOrgId());
                companyBaseInfo.setShortName(secondAgentSelectedDO.getShortName());
                companyBaseInfo.setSelectedFlag(secondAgentSelectedDO.getSelectedFlag() > 0 ? 1 : 0);
                companyBaseInfos.add(companyBaseInfo);
            }
        }
        pageCommonVO.setPageInfoList(companyBaseInfos);
        return pageCommonVO;
    }

    private String getId() throws Exception {
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult == null || textResult.getCode() != 0) {
            throw new Exception("生成ID失败!");
        }
        return textResult.getValue();
    }
}
